﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Client.Interface;


namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class EmployeeInfoView2 : BaseUserControl
    {
        private DBEntityEmployee employeeInfoEdit = null;

        public EmployeeInfoView2()
        {
           
            InitializeComponent();
            //cbxSalonID
            if (BussinessProxyManager.Instance != null)
            {
                DataSet dsSalonID;
                string error = string.Empty;
                BussinessProxyManager.Instance.QuerySalonInfo(out dsSalonID, out error);
                if (dsSalonID != null)
                {
                    foreach (DataRow dr in dsSalonID.Tables[0].Rows)
                    {
                        DevComponents.Editors.ComboItem cbxItem = new DevComponents.Editors.ComboItem();
                        cbxItem.Text = dr[DBEntitySalonInfo.FieldName.SNAME].ToString();
                        cbxItem.Tag = Convert.ToInt32(dr[DBEntitySalonInfo.FieldName.SALONID]);
                        cbxSalonID.Items.Add(cbxItem);
                    }
                    if (cbxSalonID.Items.Count > 0)
                    {
                        cbxSalonID.SelectedIndex = 0;
                    }
                }
            }
            dateEntryDate.Value = DateTime.Now;
            dateBirthday.Value = DateTime.Now;
            dateQuitDate.Value = DateTime.Now;
        }

        public DBEntityEmployee EmployeeInfo
        {
            get
            {
                return this.employeeInfoEdit;
            }
            set
            {
                this.employeeInfoEdit = value;
                if (value == null)
                {
                    return;
                }
                txtEmployeeName.Text = value.EMPLOYEENAME;
                txtName.Text = value.ENAME;
                txtECode.Text = value.ECODE;
                txtECard.Text = value.ECARD;
                dateBirthday.Value = value.EBIRTHDAY;
                txtAddress.Text = value.EADDRESS;
                txtMobilePhone.Text = value.EMOBILEPHONE;
                txtFamilyPhone.Text = value.EFAMLIYPHONE;
                txtEducation.Text = value.EDUCATION;
                dateEntryDate.Value = value.ENTRYDAY;
                cbxState.SelectedItem = DBEntityEmployee.ENUM_ESTATE.EnumStrToChineseStr(value.ESTATE);
                dateQuitDate.Value = value.EQUITDAY;
                txtIndexCode.Text = value.EINDEXCODE;
                for (int i = 0; i < cbxSalonID.Items.Count; i++)
                {
                    DevComponents.Editors.ComboItem cbxItem = (DevComponents.Editors.ComboItem)cbxSalonID.Items[i];
                    if (cbxItem.Tag.Equals(value.SALONID))
                    {
                        cbxSalonID.SelectedItem = cbxItem;
                    }
                }
                checkEmployee.Checked = (value.ISRESERVERATION == 1);
               
            }
        }
       
        private void textBoxDropDown1_Enter(object sender, EventArgs e)
        {
 
        }
       
        private void buttonX1_Click(object sender, EventArgs e)
        {
            string error = string.Empty;
            DBEntityEmployee employeeInfoEdit;
            if(!CheckInput(out error, out employeeInfoEdit)){
                MsgBox.MsgInfo(error);
                return;
            }
            bool bState = false;
            if (employeeInfoEdit == null)
            {
                //新增
                bState = BussinessProxyManager.Instance.InsertEmployee(employeeInfoEdit, out error);
            }
            else
            {
                //修改
                employeeInfoEdit.EMPLOYEEID = employeeInfoEdit.EMPLOYEEID;
                bState = BussinessProxyManager.Instance.UpdateEmployee(employeeInfoEdit, out error);
            }
            if (bState == false)
            {
                MsgBox.MsgInfo("保存员工信息错误：" + error);
            }
            else
            {
                MsgBox.MsgInfo("保存成功。");
            }
        }
        public bool CheckInput(out string error, out DBEntityEmployee employeeInfo)
        {
            employeeInfo = new DBEntityEmployee();
            error = string.Empty;

            if (txtName.Text.Equals(string.Empty))
            {
                error = "请输入员工姓名。";
                SetErrorControl(txtName, "请输入员工姓名");
                return false;
            }
            if (txtECode.Text.Equals(string.Empty))
            {
                error = "请输入员工编码。";
                SetErrorControl(txtECode, "请输入员工编码");
                return false;
            }
            if (txtECard.Text.Equals(string.Empty))
            {
                error = "请输入身份证号码。";
                SetErrorControl(txtECard, "请输入身份证号码");
                return false;
            }
            if (dateEntryDate.Value == null)
            {
                error = "请输入入职日期。";
                SetErrorControl(dateEntryDate, "请输入入职日期");
                return false;
            }
            if (dateBirthday.Value == null)
            {
                error = "请输入员工生日。";
                SetErrorControl(dateEntryDate, "请输入员工生日");
                return false;
            }
            if (txtName.Equals(string.Empty))
            {
                txtName.Text = txtEmployeeName.Text;
            }
           
            DataSet dsEmployee;
            //检测编码是否重复
            if (!BussinessProxyManager.Instance.QueryEmployeeState(DBEntityEmployee.ENUM_ESTATE.F0E, out dsEmployee, out error))
            {
                return false;
            }

            if (this.employeeInfoEdit != null)
            {//修改
                if (this.employeeInfoEdit.ECODE.Trim() != txtECode.Text.Trim())
                {
                    DataRow[] drEmployee = dsEmployee.Tables[0].Select(DBEntityEmployee.FieldName.ECODE + " = '" + txtECode.Text.Trim() + "'");
                    if (drEmployee.Length != 0)
                    {
                        error = "员工编码与现有员工重复，请重新输入。";
                        SetErrorControl(txtECode, "员工编码与现有员工重复，请重新输入");
                        return false;
                    }
                }
            }
            else
            {
                //新增
                DataRow[] drEmployee = dsEmployee.Tables[0].Select(DBEntityEmployee.FieldName.ECODE + " = '" + txtECode.Text.Trim() + "'");
                if (drEmployee.Length != 0)
                {
                    error = "员工编码与现有员工重复，请重新输入。";
                    SetErrorControl(txtECode, "员工编码与现有员工重复，请重新输入");
                    return false;
                }
            }

            employeeInfo.EMPLOYEENAME = txtEmployeeName.Text.Trim();
            employeeInfo.ENAME = txtName.Text.Trim();
            employeeInfo.ECODE = txtECode.Text.Trim();
            employeeInfo.ECARD = txtECard.Text.Trim();
            employeeInfo.EBIRTHDAY = dateBirthday.Value;
            employeeInfo.EADDRESS = txtAddress.Text.Trim();
            employeeInfo.EMOBILEPHONE = txtMobilePhone.Text.Trim();
            employeeInfo.EFAMLIYPHONE = txtFamilyPhone.Text.Trim();
            employeeInfo.EDUCATION = (txtEducation.SelectedItem == null)?"":txtEducation.SelectedItem.ToString().Trim();
            employeeInfo.ENTRYDAY = dateEntryDate.Value;
            employeeInfo.ESTATE = DBEntityEmployee.ENUM_ESTATE.ChineseStrToEnumStr(cbxState.SelectedItem.ToString());
            employeeInfo.EQUITDAY = dateQuitDate.Value;
            employeeInfo.EINDEXCODE = txtIndexCode.Text.Trim();
            DevComponents.Editors.ComboItem cbxItem = (DevComponents.Editors.ComboItem)cbxSalonID.SelectedItem;
            employeeInfo.SALONID = Convert.ToInt32(cbxItem.Tag);
            employeeInfo.EPASSWORD = string.Empty;
            employeeInfo.ISRESERVERATION = ((checkEmployee.Checked) ? 1 : 0);
            return true;

        }
        /// <summary>
        /// 按回车进入下一个控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void keyPressEdit(object sender, KeyPressEventArgs e)
        {
            base.keyPress(sender, e);
        }

        private void dateBirthday_Click(object sender, EventArgs e)
        {

        }

        private void cbxState_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxState.SelectedItem.Equals(DBEntityEmployee.ENUM_ESTATE.F0Q_CHINESE) ||
                cbxState.SelectedItem.Equals(DBEntityEmployee.ENUM_ESTATE.F0S_CHINESE))
            {
                dateQuitDate.Value = DateTime.Now;
            }
        }

    }
}
